import { TaskFieldNameMappingEnum } from '@model/enum/FieldMappingEnum.ts';
import { getPlanEndTime } from 'pub-bbx-utils';
import { safeNewDate } from '@src/util/time';
import { triggerConnectorUpdate } from '@src/util/assist';

export default {
  update({ field, newValue, oldValue }) {
    let { fieldName, displayName } = field;

    if (this.$appConfig.debug) {
      console.info(
        `[FormBuilder] ${displayName}(${fieldName}) : ${JSON.stringify(
          newValue
        )}`
      )
    }
    if(this.agendaEvent) {
      if(fieldName === 'planStartTime') {
        if(safeNewDate(newValue).getTime() > safeNewDate(this.eventValue.planEndTime).getTime()) {
          this.eventValue.planStartTime = newValue;
          this.eventValue.planEndTime = getPlanEndTime(this.planTimeConfig, newValue);
          this.$eventBus.$emit('agenda-event-plan-end-time', this.eventValue.planEndTime)
        }
        this.$eventBus.$emit('agenda-event-plan-start-time', newValue)
      } else if(fieldName === 'planEndTime') {
        if(safeNewDate(newValue).getTime() < safeNewDate(this.eventValue.planStartTime).getTime()) {
          return this.$platform.notification({
            title: this.$t('task.tip.planTimeTip7'),
            type: 'error'
          });
        }
        this.$eventBus.$emit('agenda-event-plan-end-time', newValue)
      }
    }
    let value = this.value;
    this.$set(value, fieldName, newValue);
    this.$emit('input', value);

    triggerConnectorUpdate(this, field);
    
  },
  /** 
   * @description 更新地址数据
  */
  updateAddressValue(value) {
    this.update({ field: { fieldName: TaskFieldNameMappingEnum.Address }, newValue: value });
  },
  /** 
   * @description 更新客户数据
  */
  updateCustomerValue(value) {
    this.update({ field: { fieldName: TaskFieldNameMappingEnum.Customer }, newValue: value });
  },
  /** 
   * @description 更新联系人数据
  */
  updateLinkmanValue(value) {
    this.update({ field: { fieldName: TaskFieldNameMappingEnum.Linkman }, newValue: value });
  },
  /** 
   * @description 更新客户数据
  */
  updateProductValue(value) {
    this.update({ field: { fieldName: TaskFieldNameMappingEnum.Product }, newValue: value });
  },
  /** 
   * @description 更新通知客户短信数据
  */
  updateTickValue(value) {
    this.update({ field: { fieldName: TaskFieldNameMappingEnum.Tick }, newValue: value });
  }
}